﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using RapChieuPhim.BUS;

namespace RapChieuPhim.DAO
{
    class DatVeDAO
    {
        public DatVeDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaDatVe", id);
            DataTable dt = cn.SqlExcuteQuery("LayDatVeTheoID", param);
            DatVeDTO DatVe = new DatVeDTO();

            if (dt.Rows.Count != 0)
            {
                DatVe.MaDatVe = int.Parse(dt.Rows[0]["MaDatVe"].ToString());
                DatVe.TongTien = int.Parse(dt.Rows[0]["TongTien"].ToString());
                DatVe.NguoiDat = NguoiDatBUS.GetByID(dt.Rows[0]["MaNguoiDat"].ToString());                
            }
            return DatVe;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllDatVe");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DatVeDTO DatVe = new DatVeDTO();
                    DatVe.MaDatVe = int.Parse(dr["MaDatVe"].ToString());
                    DatVe.TongTien = int.Parse(dr["TongTien"].ToString());
                    DatVe.NguoiDat = NguoiDatBUS.GetByID(dr["MaNguoiDat"].ToString());
                    array.Add(DatVe);
                }
            }
            return array;
        }

        public void Insert(DatVeDTO DatVe)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaDatVe", DatVe.MaDatVe);
            param[1] = new SqlParameter("@TongTien", DatVe.TongTien);
            param[2] = new SqlParameter("@MaNguoiDat", DatVe.NguoiDat);
            cn.SqlExcuteNonQuery("InsertDatVe", param);
        }

        public void Update(DatVeDTO DatVe)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@MaDatVe", DatVe.MaDatVe);
            param[1] = new SqlParameter("@TongTien", DatVe.TongTien);
            param[2] = new SqlParameter("@MaNguoiDat", DatVe.NguoiDat);
            cn.SqlExcuteNonQuery("UpdateDatVe", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaDatVe", id);
            cn.SqlExcuteNonQuery("DeleteDatVe", param);
        }
    }
}
